package net.lab1024.sa.base.module.support.cmdb.service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.cmdb.dao.CmdbDao;
import net.lab1024.sa.base.module.support.cmdb.domain.entity.CmdbEntity;
import net.lab1024.sa.base.module.support.cmdb.domain.form.CmdbAddForm;
import net.lab1024.sa.base.module.support.cmdb.domain.form.CmdbQueryForm;
import net.lab1024.sa.base.module.support.cmdb.domain.form.CmdbUpdateForm;
import net.lab1024.sa.base.module.support.cmdb.domain.vo.CmdbVO;
import net.lab1024.sa.base.utils.DateUtilsLocal;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * chenyuqiang Service
 *
 * @Author chenyuqiang
 * @Date 2025-10-16 19:03:13
 * @Copyright 1
 */

@Service
public class CmdbService {

    @Resource
    private CmdbDao cmdbDao;

    /**
     * 分页查询
     */
    public PageResult<CmdbVO> queryPage(CmdbQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CmdbVO> list = cmdbDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CmdbAddForm addForm) {
        CmdbEntity cmdbEntity = SmartBeanUtil.copy(addForm, CmdbEntity.class);
        cmdbEntity.setCreateTime(DateUtilsLocal.toLocalDateTime(new Date()));
        cmdbEntity.setUpdateTime(DateUtilsLocal.toLocalDateTime(new Date()));
        cmdbEntity.setCreateUser(SmartRequestUtil.getRequestUser().getUserName());
        cmdbDao.insert(cmdbEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(CmdbUpdateForm updateForm) {
        CmdbEntity cmdbEntity = SmartBeanUtil.copy(updateForm, CmdbEntity.class);
        cmdbEntity.setUpdateTime(DateUtilsLocal.toLocalDateTime(new Date()));
        cmdbEntity.setUpdateUser(SmartRequestUtil.getRequestUser().getUserName());
        cmdbDao.updateById(cmdbEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        cmdbDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        cmdbDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
